# product_details_page.py
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class ProductDetailsPage:
    # 初始化 ProductDetailsPage 对象
    def __init__(self, driver):
        self.driver = driver
        self.url = 'http://example.com/product/123'
        self.product_name_locator = (By.XPATH, '//h1[@class="product-name"]')
        self.product_description_locator = (By.ID, 'product-description')
        self.product_price_locator = (By.XPATH, '//span[@class="product-price"]')
        self.add_to_cart_button_locator = (By.ID, 'add-to-cart-button')
        self.quantity_input_locator = (By.ID, 'quantity-input')
        self.product_reviews_locator = (By.ID, 'product-reviews')
        self.review_form_locator = (By.ID, 'review-form')
        self.review_submit_button_locator = (By.ID, 'submit-review-button')

    # 导航至商品详情页面
    def navigate(self):
        self.driver.get(self.url)

    # 获取商品名称
    def get_product_name(self):
        return self.driver.find_element(*self.product_name_locator).text

    # 获取商品描述
    def get_product_description(self):
        return self.driver.find_element(*self.product_description_locator).text

    # 获取商品价格
    def get_product_price(self):
        return self.driver.find_element(*self.product_price_locator).text

    # 添加商品到购物车
    def add_to_cart(self, quantity=1):
        quantity_input = self.driver.find_element(*self.quantity_input_locator)
        quantity_input.clear()
        quantity_input.send_keys(str(quantity))
        self.driver.find_element(*self.add_to_cart_button_locator).click()

    # 获取商品评价
    def get_product_reviews(self):
        reviews = self.driver.find_elements(*self.product_reviews_locator)
        return [review.text for review in reviews]

    # 提交商品评价
    def submit_review(self, rating, comment):
        form = self.driver.find_element(*self.review_form_locator)
        form.find_element(By.ID, 'rating-input').send_keys(rating)
        form.find_element(By.ID, 'comment-input').send_keys(comment)
        form.find_element(*self.review_submit_button_locator).click()

    # 等待商品信息加载完成
    def wait_for_product_info_to_load(self):
        WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.product_name_locator)
        )